//
//  TabBarVController.m
//  电商毕业项目
//
//  Created by ibokan on 16/5/5.
//  Copyright © 2016年 ibokan. All rights reserved.
//

#import "TabBarVController.h"
#import "ShareViewController.h"
#import "SupplyAndDemandVC.h"
#import "ShoppingCartVC.h"
#import "HomeVC.h"
#import "PersonalCenterVC.h"
@interface TabBarVController ()

@end

@implementation TabBarVController
- (void)viewDidLoad {
    [super viewDidLoad];
//    YYFPSLabel *fpsLabel = [[YYFPSLabel alloc] initWithFrame:CGRectMake(0, 80, 60, 20)];
//    [self.view addSubview:fpsLabel];
    
    HomeVC *home=[[HomeVC alloc]init];
    [self rjsetUpChildWithNav:home andNorlImage:[UIImage imageNamed:@"首页未选中"]  andSelectedImage:[UIImage imageNamed:@"首页选中"] andTitle:Localized(@"购物")];
    //
    SupplyAndDemandVC *supplyAndDemand=[[SupplyAndDemandVC alloc]init];
    [self rjsetUpChildWithNav:supplyAndDemand andNorlImage:[UIImage imageNamed:@"供求未选中"]  andSelectedImage:[UIImage imageNamed:@"供求选中"] andTitle:Localized(@"供求")];

    ShareViewController *sharing=[[ShareViewController alloc]init];
    [self rjsetUpChildWithNav:sharing andNorlImage:[UIImage imageNamed:@"分享未选中"]  andSelectedImage:[UIImage imageNamed:@"分享选中"] andTitle:Localized(@"分享")];
    
    ShoppingCartVC *cart=[[ShoppingCartVC alloc]init];
    [self rjsetUpChildWithNav:cart andNorlImage:[UIImage imageNamed:@"购物车未选中"]  andSelectedImage:[UIImage imageNamed:@"购物车选中"] andTitle:Localized(@"购物车")];
    
    PersonalCenterVC *center=[[PersonalCenterVC alloc]init];
    [self rjsetUpChildWithNav:center andNorlImage:[UIImage imageNamed:@"个人中心未选中"]  andSelectedImage:[UIImage imageNamed:@"个人中心选中"] andTitle:Localized(@"我的")];
    
    //    注册登录状态监听
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginStateChange:)
                                                 name:KNOTIFICATION_LOGINCHANGE
                                               object:nil];
}
//自定义的标签栏视图方法
-(void)rjsetUpChildWithNav : (UIViewController *)child
              andNorlImage : (UIImage *)NorlImage
          andSelectedImage : (UIImage *)selectedImage
                  andTitle : (NSString *)title{
    //添加导航栏
    UINavigationController *nav=[[UINavigationController  alloc]initWithRootViewController:child];
    //    nav.navigationBar.hidden=YES;
    //设置标签栏的图片，点击时图片，标题
    child.tabBarItem.image=NorlImage;
    child.tabBarItem.selectedImage=[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    child.tabBarItem.title=title;
    //设置导航栏标题
    //    child.navigationController.navigationBar.translucent = NO;
    //    child.navigationItem.title=title;
    [self addChildViewController:nav];

}
//统一标签栏样式
+(void)initialize{
    
    UITabBarItem *tab=[UITabBarItem appearance];
    //mutableCopy将不可变数组变为可变数组
    
    //选中前字体和颜色
    NSMutableDictionary *norlDic=@{}.mutableCopy;
    norlDic[NSFontAttributeName]=[UIFont systemFontOfSize:10];
    norlDic[NSForegroundColorAttributeName]=[UIColor colorWithHexString:@"#535353"];
    [tab setTitleTextAttributes:norlDic forState:UIControlStateNormal];
    
    //选中后字体和颜色
    NSMutableDictionary *selectedDic=@{}.mutableCopy;
    selectedDic[NSFontAttributeName]=[UIFont systemFontOfSize:10];
    selectedDic[NSForegroundColorAttributeName]=[UIColor colorWithHexString:@"#ff9300"];
    [tab setTitleTextAttributes:selectedDic forState:UIControlStateSelected];
    
    //导航栏字体和颜色
    NSMutableDictionary *navDic=@{}.mutableCopy;
    navDic[NSFontAttributeName]=[UIFont systemFontOfSize:20];
    navDic[NSForegroundColorAttributeName]=[UIColor blackColor];
    [[UINavigationBar appearance]setTitleTextAttributes:navDic];
    
    //    self.navigationController.navigationBar.titleTextAttributes=navDic;
    
//    [[UINavigationBar appearance]setBarTintColor:REDColor];
    
    //设置标签栏颜色
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)loginStateChange:(NSNotification *)notification
{
    BOOL loginSuccess = [notification.object boolValue];
    //    TabBarVController *navigationController = nil;
    if (loginSuccess) {//登陆成功加载主窗口控制器
        //    if (self.mainController == nil) {
        //        self.mainController = [[TabBarVController alloc] init];
        //        self.window.rootViewController = self.mainController;
        //    }else{
        //        self.window.rootViewController  = self.mainController;
        //    }
    }
    else{//登陆失败加载登陆页面控制器
        ViewController *loginController=[[ViewController alloc]init];
        UINavigationController * nav = [[UINavigationController alloc]   initWithRootViewController:loginController];
        [self presentViewController:nav animated:YES completion:nil];
        //        UINavigationController  *nav=[[UINavigationController alloc]initWithRootViewController:loginController];
        ////        nav.navigationBar.accessibilityIdentifier = @"navigationbar";
        //        self.window.rootViewController=nav;
    }
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

